/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ihale.satinalmaci;

import java.io.IOException;
import java.sql.SQLException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import org.ihale.database.Database;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
/**
 *
 * @author yakup
 */
@ManagedBean(name="satinalmacibean")
@SessionScoped
public class SatinAlmaciBean {
   private SatinAlmaci satinAlmaci;
   private String satinalmaci_id;
   private SatinAlmaci getirilenSatinAlmaci;
   private SatinAlmaci secilenSatinalmaci;
   private List<SatinAlmaci> satinAlmaciListesi;

    public SatinAlmaci getSecilenSatinalmaci() {
        return secilenSatinalmaci;
    }

    public void setSecilenSatinalmaci(SatinAlmaci secilenSatinalmaci) {
        this.secilenSatinalmaci = secilenSatinalmaci;
    }
   
    public SatinAlmaci getGetirilenSatinAlmaci() {
        return getirilenSatinAlmaci;
    }

    public void setGetirilenSatinAlmaci(SatinAlmaci getirilenSatinAlmaci) {
        this.getirilenSatinAlmaci = getirilenSatinAlmaci;
    }
   
    public String getSatinalmaci_id() {
        return satinalmaci_id;
    }

    public void setSatinalmaci_id(String satinalmaci_id) {
        this.satinalmaci_id = satinalmaci_id;
    }

   
    public List<SatinAlmaci> getSatinAlmaciListesi() {
        return satinAlmaciListesi;
    }

   
    public SatinAlmaci getSatinAlmaci() {
        return satinAlmaci;
    }

    public void setSatinAlmaci(SatinAlmaci satinAlmaci) {
        this.satinAlmaci = satinAlmaci;
    }
     
    public SatinAlmaciBean(SatinAlmaci satinAlmaci) {
        this.satinAlmaci=satinAlmaci;
       
    }
    
 public SatinAlmaciBean() {
       this.satinAlmaci=new SatinAlmaci();
       this.satinAlmaciListesi= new ArrayList<SatinAlmaci>();
       this.secilenSatinalmaci= new SatinAlmaci();
      Database database = new Database();
       try{
        database.connect();
           this.satinAlmaciListesi=database.ListeSatinalmaci();
        database.disconnect();
       } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Liste veri tabanından alınamadı!"));
            System.out.println(e.toString());
	}
       
    
 }
 
 public void login() throws ClassNotFoundException, SQLException, IOException{
     
     Database db = new Database();
     db.connect();
     satinAlmaci = db.getSatinAlmaci(satinAlmaci);
     if(satinAlmaci != null){
            FacesContext.getCurrentInstance().getExternalContext().redirect("faces/SatinAlmaci/SatinAlmaciAnaSayfa.xhtml");
        }
        else{
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Username and/or password is wrong. Please try again !"));
        }
               
        db.disconnect();
 }
    
     public void  ekleSatinalmaci()
  {
   Database database = new Database();
    	try {
            database.connect();
            if(database.ekleSatinalmaci(this.satinAlmaci))
            {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı eklendi."));
                 this.satinAlmaciListesi=database.ListeSatinalmaci();
            }
            else
             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı eklenemedi."));
                
                database.disconnect();
           
	} catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Kaldırma işlemi başarısız."));
            System.out.println(e.toString());
	}
  }
     
     public void getirSatinalmaci( )
     {
      Database database = new Database();
      	
      try{
            database.connect();
            this.getirilenSatinAlmaci=database.getirSatinalmaci(this.satinalmaci_id);
             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı eklendi."));
            
             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı eklenemedi."));
                
                database.disconnect();
           
	} catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Kaldırma işlemi başarısız."));
            System.out.println(e.toString());
	}
     
     }
     
 
     public void silSatinalmaci( )
     {
      Database database = new Database();
      	
      try{
            database.connect();
            if(database.silSatinalmaci(this.secilenSatinalmaci))
             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı silindi."));
            else
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Silinemedi!!!."));
            
            this.satinAlmaciListesi=database.ListeSatinalmaci();
        
            database.disconnect();
	} catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Silinemedi!!!."));
            System.out.println(e.toString());
	}
     
     
     }
    
     
      public void güncelleSatinalmaci( )
     {
      Database database = new Database();
      	
      try{
            database.connect();
            if(database.guncelleSatinalmaci(this.secilenSatinalmaci))
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı güncellendi!"));
            else
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Satın almacı güncellenemedi!"));
                     
            this.satinAlmaciListesi=database.ListeSatinalmaci();
                
                database.disconnect();
           
	} catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Güncellenemedi!!!."));
            System.out.println(e.toString());
	}
     
     }
}
